package cn.cds.withyou.lib.image.impl;

import android.graphics.Bitmap;
import android.view.View;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.lang.ref.WeakReference;

import cn.cds.withyou.lib.image.ImageListener;

public class UilImageLoaderAdapter
{
    public static ImageLoadingListener converseTo(ImageListener listener)
    {
        if (listener != null)
        {
            final WeakReference<ImageListener> weakRef = new WeakReference(listener);
            return new ImageLoadingListener()
            {
                public void onLoadingStarted(String imageUri, View view)
                {
                    if ((weakRef != null) && (weakRef.get() != null)) {
                        weakRef.get().onLoadingStarted(imageUri, view);
                    }
                }

                public void onLoadingFailed(String imageUri, View view, FailReason arg2)
                {
                    if ((weakRef != null) && (weakRef.get() != null)) {
                        weakRef.get().onLoadingFailed(imageUri, view, null);
                    }
                }

                public void onLoadingComplete(String imageUri, View view, Bitmap arg2)
                {
                    if ((weakRef != null) && (weakRef.get() != null)) {
                        weakRef.get().onLoadingComplete(imageUri, view, arg2);
                    }
                }

                public void onLoadingCancelled(String imageUri, View view)
                {
                    if ((weakRef != null) && (weakRef.get() != null)) {
                        weakRef.get().onLoadingCancelled(imageUri, view);
                    }
                }
            };

        }
        return null;
    }
}
